package com.qyhl.module_practice.substreet.show;

import android.os.Bundle;
import android.text.style.TextAppearanceSpan;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.gyf.immersionbar.ImmersionBar;
import com.makeramen.roundedimageview.RoundedImageView;
import com.qyhl.module_practice.R;
import com.qyhl.module_practice.R2;
import com.qyhl.module_practice.substreet.fragment.SubStreetContract;
import com.qyhl.module_practice.substreet.fragment.SubStreetPresenter;
import com.qyhl.webtv.basiclib.base.BaseActivity;
import com.qyhl.webtv.basiclib.base.BaseIViewPresenter;
import com.qyhl.webtv.basiclib.utils.NetUtil;
import com.qyhl.webtv.basiclib.utils.SpanUtils;
import com.qyhl.webtv.basiclib.utils.StringUtils;
import com.qyhl.webtv.commonlib.constant.ARouterPathConstant;
import com.qyhl.webtv.commonlib.entity.civilized.PracticeListBean;
import com.qyhl.webtv.commonlib.utils.router.RouterManager;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.weavey.loading.lib.LoadingLayout;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

@Route(path = ARouterPathConstant.PRACTICE_SUB_STREET_SHOW)
public class SubStreetShowActivity extends BaseActivity implements SubStreetContract.SubStreetView {
    @BindView(R2.id.title)
    TextView title;
    @BindView(R2.id.toolbar)
    Toolbar toolbar;
    @BindView(R2.id.recycle_view)
    RecyclerView recycleView;
    @BindView(R2.id.refresh)
    SmartRefreshLayout refresh;
    @BindView(R2.id.load_mask)
    LoadingLayout loadMask;

    private SubStreetPresenter mPresenter;

    private CommonAdapter commonAdapter;

    private List<PracticeListBean> mDataList = new ArrayList<>();

    private int page = 1;

    private String instId;

    private String type = "1";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    protected int getContentView() {
        return R.layout.practice_activity_sub_street_show;
    }

    @Override
    protected void setImmersionBar(ImmersionBar mImmersionBar) {
        setDefaultImmerBar();
    }

    @Override
    protected BaseIViewPresenter initBaseIViewPresenter() {
        return null;
    }

    @Override
    protected void init() {
        mPresenter = new SubStreetPresenter(this);
        instId = getIntent().getStringExtra("id");
        String titleName = getIntent().getStringExtra("title");

        setSupportActionBar(toolbar);
        if (StringUtils.isNotEmpty(titleName)) {
            title.setText(titleName);
        } else {
            title.setText("实践阵地");
        }

        initView();

    }

    @Override
    protected void setListener() {
        setListeners();
    }

    private void initView() {
        loadMask.setStatus(LoadingLayout.Loading);

        refresh.setRefreshHeader(new MaterialHeader(this));
        refresh.setRefreshFooter(new ClassicsFooter(this));
        refresh.setEnableLoadMore(true);

        //设置布局管理器
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recycleView.setLayoutManager(linearLayoutManager);

        recycleView.setAdapter(commonAdapter = new CommonAdapter<PracticeListBean>(this, R.layout.practice_item_street_list_show, mDataList) {
            @Override
            protected void convert(ViewHolder holder, PracticeListBean bean, int position) {
                RoundedImageView cover = holder.getView(R.id.cover);
                Glide.with(SubStreetShowActivity.this)
                        .load(bean.getLogo())
                        .apply(new RequestOptions().placeholder(R.drawable.cover_normal_default).error(R.drawable.cover_normal_default))
                        .into(cover);

                holder.setText(R.id.title, bean.getName());

                holder.setText(R.id.score, "爱心积分：" + bean.getScore() + "分");

                holder.setText(R.id.act, "服务项目：" + bean.getActTotal() + "场");

                holder.setText(R.id.vol, "志愿者：" + bean.getVolTotal() + "人");

                holder.setText(R.id.adress, bean.getAddress());

                holder.setText(R.id.phone, bean.getTelephoneNum());

                if (bean.getAct() != null && bean.getAct().getStatus() != null) {
                    holder.setVisible(R.id.activity, true);
                    holder.setVisible(R.id.divider_line, true);
                    TextView activity = holder.getView(R.id.activity);
                    SpanUtils spanUtils = new SpanUtils(SubStreetShowActivity.this);
                    switch (bean.getAct().getStatus()) {
                        case "NOT_BEGIN":
                            spanUtils.append("未开始：").setSpans(new TextAppearanceSpan(SubStreetShowActivity.this, R.style.practice_activity_txt_on));
                            break;
                        case "RECRUIT":
                            spanUtils.append("招募中：").setSpans(new TextAppearanceSpan(SubStreetShowActivity.this, R.style.practice_activity_txt_on));
                            break;
                        case "SERVICE":
                            spanUtils.append("服务中：").setSpans(new TextAppearanceSpan(SubStreetShowActivity.this, R.style.practice_activity_txt_on));
                            break;
                        case "BEGINNING":
                            spanUtils.append("进行中：").setSpans(new TextAppearanceSpan(SubStreetShowActivity.this, R.style.practice_activity_txt_on));
                            break;
                        case "END":
                            spanUtils.append("已结束：").setSpans(new TextAppearanceSpan(SubStreetShowActivity.this, R.style.practice_activity_txt_on));
                            break;
                        default:
                            break;
                    }
                    spanUtils.append(bean.getAct().getName()).setSpans(new TextAppearanceSpan(SubStreetShowActivity.this, R.style.practice_activity_txt_off));
                    activity.setText(spanUtils.create());
                } else {
                    holder.setVisible(R.id.activity, false);
                    holder.setVisible(R.id.divider_line, false);
                }

            }
        });

        mPresenter.getList(instId, type, page + "");
    }

    private void setListeners() {
        loadMask.setOnReloadListener(new LoadingLayout.OnReloadListener() {
            @Override
            public void onReload(View v) {
                loadMask.setReloadButtonText("加载中...");
                page = 1;
                mPresenter.getList(instId, type, page + "");
            }
        });

        refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                mPresenter.getList(instId, type, page + "");
            }
        });

        refresh.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                mPresenter.getList(instId, type, page + "");
            }
        });

        commonAdapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {

                Bundle bundle = new Bundle();
                bundle.putString("instId", instId);
                bundle.putString("streetId", mDataList.get(position).getId() + "");
                bundle.putString("title", mDataList.get(position).getName());
                RouterManager.router(ARouterPathConstant.PRACTICE_STREET_DETAIL,bundle);

            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });
    }


    @Override
    public void setList(List<PracticeListBean> beanList, boolean isMore) {
        loadMask.setReloadButtonText("点击重试~");
        if (isMore) {
            refresh.finishLoadMore();
        } else {
            loadMask.setStatus(LoadingLayout.Success);
            refresh.finishRefresh();
            mDataList.clear();
        }
        page++;
        mDataList.addAll(beanList);
        commonAdapter.notifyDataSetChanged();
    }

    @Override
    public void setError(String msg, boolean isMore) {
        loadMask.setReloadButtonText("点击重试~");
        if (isMore) {
            refresh.finishLoadMore();
            if (!NetUtil.isNetworkAvalible(this)) {
                showToast("网络异常，请检查您的网络！", 4);
            }
        } else {
            refresh.finishRefresh();
            loadMask.setStatus(LoadingLayout.Error);
            if (!NetUtil.isNetworkAvalible(this)) {
                loadMask.setErrorImage(R.drawable.error_network);
                loadMask.setErrorText("网络异常，请检查您的网络！");
            } else if (msg.contains("暂无")) {
                loadMask.setErrorImage(R.drawable.empty_content);
                loadMask.setErrorText(msg);
            } else {
                loadMask.setErrorImage(R.drawable.error_content);
                loadMask.setErrorText(msg);
            }
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
